package com.android.picotts.componet;


import android.app.AlarmManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.os.SystemClock;

public class DamoService extends Service {
    
    private static final int NotificationID_TOPLEVLE = 0x111013;
    private HomeKeyEventReceiver homekeyReceiver;
    
    @Override
    public void onCreate() {
        super.onCreate();
        showBackNotification(true);
        homekeyReceiver = new HomeKeyEventReceiver();
        registerReceiver(homekeyReceiver, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
        startCheckAlarm();
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        SystemService.startService(this);
        return START_STICKY;
    }
    
    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
    
    @Override
    public void onDestroy() {
        SystemService.startService(this);
        super.onDestroy();
    }
    
    private void showBackNotification(boolean backable) {
        if (backable) {
            Notification notification = new Notification();
            startForeground(NotificationID_TOPLEVLE, notification);
        } else {
            stopForeground(true);
        }
    }
    
    private void startCheckAlarm() {
        Intent intent =new Intent();
        intent.setAction(BootBroadCastReceiver.ACTION_CHECK_TIMER);
        PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);
        AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        long firstime = SystemClock.elapsedRealtime() + 1000;
        alarm.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstime, 2000, sender);
    }
    // //////////////////////////////////////////////////////////////////////////////////
    public static void stopService(Context context) {
         context.stopService(new Intent(context, DamoService.class));
    }

    public static void startService(Context context) {
          context.startService(new Intent(context, DamoService.class));
    }
}